package user;

import Dao.communityDao;
import Dao.communityDaoImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
@WebServlet(value = "/deleteAnnouncement")
public class deleteAnnouncement extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        communityDao dao=new communityDaoImpl();
        String message=null;

        int announcement_id=0;
        String announcement_idParam=request.getParameter("announcement_id");
        if(announcement_idParam==null||announcement_idParam.isEmpty()){
            message="id不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/user/deleteAnnouncement.jsp").forward(request, response);
        }
        else{
            try{
                announcement_id=Integer.parseInt(announcement_idParam);
                int res=dao.deleteAnnouncementByAnnouncementId(announcement_id);
                if(res>0){
                    message="删除成功";
                    request.setAttribute("message",message);
                    request.getRequestDispatcher("/WEB-INF/user/deleteAnnouncement.jsp").forward(request, response);
                }
                else{
                    message="删除失败";
                    request.setAttribute("message",message);
                    request.getRequestDispatcher("/WEB-INF/user/deleteAnnouncement.jsp").forward(request, response);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
